package com.winning.sjzc.sjypz.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.winning.sjzc.common.utils.DateUtil;
import com.winning.sjzc.sjypz.dao.SjzcSjkxxMapper;
import com.winning.sjzc.sjypz.entity.SjzcSjkxxEntiy;
import com.winning.sjzc.sjypz.service.SjzcSjkxxService;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.winning.sjzc.sjypz.utils.DbConnectUtils;
import com.winning.sjzc.sjypz.utils.DynamicDataSourceConfig;
import com.winning.sjzc.sjypz.utils.SqlExecutorUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;

/**
 * description:
 *
 * @author yanxf@winning.com.cn
 * @date 2019/08/15 9:59
 */
@Service
public class SjzcSjkxxServiceImpl implements SjzcSjkxxService {

  @Autowired
  private SjzcSjkxxMapper sjzcSjkxxMapper;

  private DbConnectUtils dbUtils = DbConnectUtils.getInstance();

  @Override
  public String getDriverNameByDbType(String dbtype) {
    return dbUtils.getDriverName(dbtype);
  }

  @Override
  public Connection getDruidConnection(SjzcSjkxxEntiy sjkxxEntiy) throws SQLException {
    String dbtype = sjkxxEntiy.getSjklx();
    String yhm = sjkxxEntiy.getYhm();
    String mm = sjkxxEntiy.getMm();
    String drivername = getDriverNameByDbType(dbtype);
    String dbIp = sjkxxEntiy.getSjkdz();
    String dbInstance = sjkxxEntiy.getSjkdm();
    String jdbcUrl = dbUtils.getConnectUrl(dbtype,dbIp,dbInstance);
    DynamicDataSourceConfig dataSourceConfig = new DynamicDataSourceConfig(drivername,jdbcUrl,yhm,mm);
    DataSource dataSource = dataSourceConfig.getDataSource();
    return dataSource.getConnection();
  }

  public boolean testDbConnect(SjzcSjkxxEntiy sjzcSjkxxEntiy) throws Exception {
    Connection conn = null;
    String dbtype = sjzcSjkxxEntiy.getSjklx();
    String dbIp = sjzcSjkxxEntiy.getSjkdz();
    String dbInstance = sjzcSjkxxEntiy.getSjkdm();
    String username = sjzcSjkxxEntiy.getYhm();
    String password = sjzcSjkxxEntiy.getMm();
    String jdbcUrl = dbUtils.getConnectUrl(dbtype,dbIp,dbInstance);
    conn = dbUtils.initDbConnect(jdbcUrl,username,password,dbtype);
    if(null == conn){
      return false;
    }
    return true;
  }

  private void testCon() throws SQLException, ClassNotFoundException {
    String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";          //获取mysql数据库的驱动类
    String url="jdbc:sqlserver://172.16.0.29;DatabaseName=PT_STORE_JCGL"; //连接数据库,album为数据库名称
    String username = "sa";
    String pw = "sql2k8";
    Class.forName(driver);
    Connection conn=DriverManager.getConnection(url,username,pw);//获取连接对象
  }

  @Override
  public SjzcSjkxxEntiy getSjkxxBySjkdm(String sjkdm) {
    return sjzcSjkxxMapper.getSjkxxBySjkdm(sjkdm);
  }

  @Override
  public int save(SjzcSjkxxEntiy sjzcSjkxxEntiy) {
    Date cjsj = DateUtil.getDate();
    sjzcSjkxxEntiy.setCjsj(cjsj);
    sjzcSjkxxEntiy.setGxsj(cjsj);
    sjzcSjkxxMapper.insert(sjzcSjkxxEntiy);
    return 1;
  }

  @Override
  public void delete(String uid) {
    sjzcSjkxxMapper.deleteById(uid);
  }

  @Override
  public List<SjzcSjkxxEntiy> getByEntity(SjzcSjkxxEntiy entity) {
    HashMap<String, Object> map = new HashMap<>(2);
    if (StringUtils.isNotEmpty(entity.getSjkmc())) {
      map.put("SJKMC", entity.getSjkmc());
    }
    if (StringUtils.isNotEmpty(entity.getSjkdm())) {
      map.put("SJKDM", entity.getSjkdm());
    }

    return sjzcSjkxxMapper.selectByMap(map);
  }

  @Override
  public List<SjzcSjkxxEntiy> findAll() {
    return sjzcSjkxxMapper.selectList(new QueryWrapper<SjzcSjkxxEntiy>());
  }

  @Override
  public int updateSjkxx(SjzcSjkxxEntiy sjzcSjkxxEntiy) {
    return sjzcSjkxxMapper.update(sjzcSjkxxEntiy,new UpdateWrapper<SjzcSjkxxEntiy>().eq("sjkdm",sjzcSjkxxEntiy.getSjkdm()));
  }

  @Override
  public int delBySjkdm(String sjkdm) {
    Map<String,Object> columnMap = new HashMap<>();
    columnMap.put("sjkdm",sjkdm);
    return sjzcSjkxxMapper.deleteByMap(columnMap);
  }
}
